﻿/**
 * Created by Administrator on 2017/1/4.
 */
var gulp = require('gulp');//可通过npmjs.com查询插件用法
var $ = require('gulp-load-plugins')();
var open = require('open');

var app = {
    srcPath: 'src/',
    devPath: 'build/',
    prdPath: 'dist/'
}

gulp.task('lib', function () {
    gulp.src('bower_components/**/*')
        .pipe(gulp.dest(app.devPath + 'vendor'))
        .pipe(gulp.dest(app.prdPath + 'vendor'))
        .pipe($.connect.reload());
});
gulp.task('html', function () {
    gulp.src(app.srcPath + '**/*.html')
        .pipe(gulp.dest(app.devPath))
        .pipe(gulp.dest(app.prdPath))
        .pipe($.connect.reload());
});
gulp.task('json', function () {
    gulp.src(app.srcPath + 'data/**/*.json')
        .pipe(gulp.dest(app.devPath + 'data/'))
        .pipe(gulp.dest(app.prdPath + 'data/'))
        .pipe($.connect.reload());
});
gulp.task('css', function () {
    const mapsfiles = $.filter(['**/*','!maps/*'],{restore:true});
    gulp.src(app.srcPath + 'style/index.less')
        .pipe($.plumber())
        .pipe($.sourcemaps.init())
        .pipe($.less())
        .pipe($.autoprefixer({
            browsers: ['last 10 versions', 'Android >= 4.0'],
            cascade: true, //是否美化属性值 默认：true 像这样：
            //-webkit-transform: rotate(45deg);
            //        transform: rotate(45deg);
            remove:true //是否去掉不必要的前缀 默认：true
        }))
        .pipe($.sourcemaps.write('maps/'))
        .pipe(gulp.dest(app.devPath + 'css/'))
        .pipe(mapsfiles)
        .pipe($.cssmin())
        .pipe(mapsfiles.restore)
        .pipe(gulp.dest(app.prdPath + 'css/'))
        .pipe($.connect.reload());
});
gulp.task('js', function () {
    gulp.src(app.srcPath + 'script/**/*.js')
        .pipe($.plumber())
        .pipe($.concat('index.js'))
        .pipe(gulp.dest(app.devPath + 'js/'))
        .pipe($.uglify())
        .pipe($.rev())
        .pipe(gulp.dest(app.prdPath + 'js/'))
        .pipe($.rev.manifest())
        .pipe(gulp.dest('rev/'))
        .pipe($.connect.reload());
});
gulp.task('rev', function () {
    gulp.src(['rev/*.json',app.prdPath + '**/*.html'])
        .pipe($.revCollector())
        .pipe(gulp.dest(app.prdPath))
        .pipe($.connect.reload());
});

gulp.task('image', function () {
    gulp.src(app.srcPath + 'images/**/*')
        .pipe($.plumber())
        .pipe(gulp.dest(app.devPath + 'images/'))
        .pipe($.imagemin())
        .pipe(gulp.dest(app.prdPath + 'images/'))
        .pipe($.connect.reload());
});
gulp.task('build',['lib', 'html', 'json', 'css', 'js', 'rev', 'image'])
gulp.task('clean', function () {
    gulp.src([app.devPath, app.prdPath])
        .pipe($.clean());
});
gulp.task('server',['build'], function () {
    $.connect.server({
        root: [app.devPath],
        livereload: true,
        port: 12345
    });
    open('http://localhost:12345');
    gulp.watch(app.srcPath + 'bower_components/**/*', ['lib']);
    gulp.watch(app.srcPath + '**/*.html', ['html']);
    gulp.watch(app.srcPath + 'style/**/*.less', ['css']);
    gulp.watch(app.srcPath + 'script/**/*.js', ['js','rev']);
    gulp.watch(app.srcPath + 'images/**/*', ['image']);
    gulp.watch(app.srcPath + 'data/**/*', ['json']);
});

/*
gulp.task('default', ['server'], function () {
    return gulp.src('./css/!*.scss', {base: './'})
        .pipe(sass({outputStyle: 'compact'}))
        .pipe(gulp.dest('dist/'));
});*/
gulp.task('default', ['server']);